package com.reremouse.rerebbs.web.mb.mag;

import com.reremouse.model.AdminInfo;
import com.reremouse.rerebbs.dao.IAdminDAO;
import com.reremouse.rerebbs.dao.ISystemConfigDAO;
import com.reremouse.util.SpringHelper;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;

@ManagedBean
@ViewScoped
public class AdminLogin
        implements Serializable {
    AdminInfo user = new AdminInfo();
    String authStr = null;
    String backSiteName = "";
    IAdminDAO adminDAO = (IAdminDAO) SpringHelper.getSpringBean("AdminDAO");
    ISystemConfigDAO scDAO = (ISystemConfigDAO) SpringHelper.getSpringBean("SystemConfigDAO");
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    @ManagedProperty("#{adminSessionBean}")
    AdminSessionBean adminSessionBean;
    long sTime = System.nanoTime();

    public AdminSessionBean getAdminSessionBean() {
        return this.adminSessionBean;
    }

    public void setAdminSessionBean(AdminSessionBean adminSessionBean) {
        this.adminSessionBean = adminSessionBean;
    }

    public String getAuthStr() {
        return this.authStr;
    }

    public void setAuthStr(String authStr) {
        this.authStr = authStr;
    }

    public AdminInfo getUser() {
        return this.user;
    }

    public void setUser(AdminInfo user) {
        this.user = user;
    }

    public long getSTime() {
        return this.sTime;
    }

    public void setSTime(long sTime) {
        this.sTime = sTime;
    }

    public String getBackSiteName() {
        return this.backSiteName;
    }

    public void setBackSiteName(String backSiteName) {
        this.backSiteName = backSiteName;
    }

    @PostConstruct
    public void init() {
        this.authStr = "";
        this.backSiteName = this.scDAO.getSystemConfig().getSiteName();
    }

    public String login_action() {
        String rand = (String) this.session.getAttribute("rand");
        FacesMessage fm = new FacesMessage();
        fm.setSeverity(FacesMessage.SEVERITY_ERROR);
        System.out.println("I'm here.");
        if (rand != null) {
            if (!rand.toLowerCase().equals(this.authStr.toLowerCase())) {
                fm.setSummary("验证码错误！");
                this.authStr = "";
                FacesContext.getCurrentInstance().addMessage("", fm);
                return null;
            }
        } else {
            fm.setSummary("验证码错误！");
            FacesContext.getCurrentInstance().addMessage("", fm);
            return null;
        }
        AdminInfo admin = this.adminDAO.findAdminByUrn(this.user.getUrn());
        if (admin == null) {
            fm.setSummary("this user is not exist");
            this.authStr = "";
            FacesContext.getCurrentInstance().addMessage("", fm);
            return null;
        }
        if (!admin.getPwd().toLowerCase().equals(this.user.getPwd().toLowerCase())) {
            fm.setSummary("the pass is error");
            this.authStr = "";
            FacesContext.getCurrentInstance().addMessage("", fm);
            return null;
        }
        if (!admin.getEnabled().booleanValue()) {
            fm.setSummary("sorry, this user is not active");
            this.authStr = "";
            FacesContext.getCurrentInstance().addMessage("", fm);
            return null;
        }
        AdminSessionBean asb = this.adminSessionBean;
        asb.setAdmin(admin);
        asb.setIfLogin(true);
        boolean t = admin.getGrp().equals("admin");
        asb.setIfSuper(t);
        asb.setLtime(admin.getLtime());

        admin.setLtime(new Date());
        this.adminDAO.updateAdmin(admin);


        return "/m/Main?faces-redirect=true";
    }

    public String refresh() {
        this.authStr = "";
        this.sTime = System.nanoTime();
        return null;
    }
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.rerebbs.web.mb.mag.AdminLogin

 * JD-Core Version:    0.7.0.1

 */